-- Keymaps are automatically loaded on the VeryLazy event
-- Default keymaps that are always set: https://gitcode.com/Cangjie-SIG/CJ-NVIM/blob/main/nvim/lua/config/keymaps.lua
-- Add any additional keymaps here

-- NvimTree Toggle
-- vim.keymap.set('n', '<leader>e', ':NvimTreeToggle<CR>', { noremap = true, silent = true })

-- 仓颉文件专用快捷键(https://gitcode.com/ystyle/cangjie-nvim)
vim.api.nvim_create_autocmd("LspAttach", {
  callback = function(args)
    if vim.bo[args.buf].filetype ~= "Cangjie" then return end
    local opts = { buffer = args.buf }
    
    -- 使用本地函数简化映射
    local function map(mode, lhs, rhs, desc) vim.keymap.set(mode, lhs, rhs, { buffer = args.buf, desc = desc }) end
    map("n", "<leader>li", "<cmd>LspInfo<cr>", "LSP Info")
    map("n", "<leader>lI", "<cmd>NullLsInfo<cr>", "Null-ls Info")
    map("n", "K", vim.lsp.buf.hover, "Hover Document")
    map("n", "<leader>lf", function() vim.lsp.buf.format { async = true } end, "Format Document")
    -- map("n", "<leader>lS", "<cmd>SymbolsOutline<cr>", "Symbols Outline") -- 这个会冲突注释掉了，但是不影响使用
    map("n", "gl", function() vim.diagnostic.open_float() end, "Line Diagnostics")
    map("n", "<leader>ld", function() vim.diagnostic.open_float() end, "Line Diagnostics")
    map("n", "<C-W>d", function() vim.diagnostic.open_float() end, "Line Diagnostics")
    map("n", "<leader>lD", function() vim.diagnostic.setqflist() end, "All Diagnostics")
    map("n", "gra", function() vim.lsp.buf.code_action() end, "Code Actions")
    map("n", "<leader>la", function() vim.lsp.buf.code_action() end, "Code Actions")
    map("n", "<leader>lA", function() vim.lsp.buf.range_code_action() end, "Source Code Actions")
    map("n", "<leader>lh", function() vim.lsp.buf.signature_help() end, "Signature Help")
    map("n", "grn", function() vim.lsp.buf.rename() end, "Rename")
    map("n", "<leader>lr", function() vim.lsp.buf.rename() end, "Rename")
    map("n", "<leader>ls", function() vim.lsp.buf.document_symbol() end, "Document Symbols")
    map("n", "<leader>lG", function() vim.lsp.buf.workspace_symbol() end, "Workspace Symbols")
    map("n", "]d", function() vim.diagnostic.goto_next() end, "Diagnostic Next")
    map("n", "[d", function() vim.diagnostic.goto_prev() end, "Diagnostics Previous")
    map(
      "n",
      "]e",
      function() vim.diagnostic.goto_next { severity = vim.diagnostic.severity.ERROR } end,
      "Diagnostic Error Next"
    )
    map(
      "n",
      "[e",
      function() vim.diagnostic.goto_prev { severity = vim.diagnostic.severity.ERROR } end,
      "Diagnostic Error Previous"
    )
    map(
      "n",
      "]w",
      function() vim.diagnostic.goto_next { severity = vim.diagnostic.severity.WARN } end,
      "Diagnostic Warning Next"
    )
    map(
      "n",
      "[w",
      function() vim.diagnostic.goto_prev { severity = vim.diagnostic.severity.WARN } end,
      "Diagnostic Warning Previous"
    )
    map("n", "]y", function() vim.lsp.buf.document_symbol() end, "Document Symbol Next")
    map("n", "[y", function() vim.lsp.buf.document_symbol() end, "Document Symbol Previous")
    map("n", "gO", function() vim.lsp.buf.document_symbol() end, "Document Symbol")
    map("n", "gD", function() vim.lsp.buf.declaration() end, "Declaration")
    map("n", "gy", function() vim.lsp.buf.type_definition() end, "Type Definition")
    map("n", "gd", function() vim.lsp.buf.definition() end, "Definition")
    map("n", "gri", function() vim.lsp.buf.implementation() end, "Implementation")
    map("n", "grr", function() vim.lsp.buf.references() end, "References") -- 这两个在我机器上会导致lsp崩溃
    map("n", "<leader>lR", function() vim.lsp.buf.references() end, "References")
  end,
})
